//Translit-Script-Start

ChCaps=false

list=[{A:"А",a:"а",B:"Б",b:"б",V:"В",v:"в",G:"Г",g:"г",D:"Д",d:"д",E:"Е",e:"е",Ë:"Ё",ë:"ё",Ž:"Ж",
                ž:"ж",Z:"З",z:"з",I:"И",i:"и",J:"Й",j:"й",K:"К",k:"к",L:"Л",l:"л",M:"М",m:"м",N:"Н",n:"н",O:"О",o:"о",P:"П",p:"п",R:"Р",r:"р",
                S:"С",s:"с",T:"Т",t:"т",U:"У",u:"у",F:"Ф",f:"ф",H:"Х",h:"х",C:"Ц",c:"ц",Č:"Ч",č:"ч",Š:"Ш",š:"ш",Ŝ:"Щ",ŝ:"щ","\u0022":"ъ","ъъ":"Ъ",
                Y:"Ы",y:"ы","\u0027":"ь","ьь":"Ь",È:"Э",è:"э",Û:"Ю",û:"ю",Â:"Я",â:"я"
      },
      {А:"A",а:"a",Б:"B",б:"b",В:"V",в:"v",Г:"G",г:"g",Д:"D",д:"d",Е:"E",е:"e",Ё:"Ë",ё:"ë",Ж:"Ž",ж:"ž",З:"Z",з:"z",И:"I",и:"i",Й:"J",й:"j",К:"K",
                к:"k",Л:"L",л:"l",М:"M",м:"m",Н:"N",н:"n",О:"O",о:"o",П:"P",п:"p",Р:"R",р:"r",С:"S",с:"s",Т:"T",т:"t",У:"U",у:"u",Ф:"F",ф:"f",Х:"H",
                х:"h",Ц:"C",ц:"c",Ч:"Č",ч:"č",Ш:"Š",ш:"š",Щ:"Ŝ",щ:"ŝ",Ъ:"\"\"",ъ:"\"",Ы:"Y",ы:"y",Ь:"''",ь:"'",Э:"È",э:"è",Ю:"Û",ю:"û",Я:"Â",я:"â"
      }
     ]

function put_char(x) {
    if (ChCaps) {
        x--;
    }
    var cnt = 0;
    var chr;
    for (i in list[1]) {
        cnt++;
        if (cnt == x)
            chr = i;
    }
    var text_var = document.getElementById("textarea_field");
    if (document.selection) {
        text_var.focus();
        var range = document.selection.createRange();
        range.text = chr;
        range.select();
    }
    else
    if (text_var.selectionStart) {
        var top = text_var.scrollTop;
        var start = text_var.selectionStart;
        var end = text_var.selectionEnd;
        var left= text_var.value.substring(0, start);
        var right =text_var.value.substring(end, text_var.value.length);

        text_var.value = left + chr + right;
        text_var.selectionStart = start + 1;
        text_var.selectionEnd = start + 1;
        text_var.scrollTop=top;
    }
    else {
        text_var.value += chr;
    }
    text_var.focus();
}

function caps_char() {
    if(ChCaps){
        document.getElementById("russian_keyboard_content").style.textTransform="none";
        document.getElementById("Caps_id").FclassName="off";
    }
    else
    {
        document.getElementById("russian_keyboard_content").style.textTransform="uppercase";
        document.getElementById("Caps_id").className="on";
    }
    ChCaps=!ChCaps;
}

function translit(y){
    var left,right;
    var text="";   
    var text_var=document.getElementById("textarea_field");
    if(text_var.value=="")
        return;
    
    if (text_var.selectionStart >= 0) {
        var top=text_var.scrollTop;
        var start = text_var.selectionStart;
        var end = text_var.selectionEnd;
        if (start != end) {
            left = text_var.value.substring(0, start);
            right = text_var.value.substring(end, text_var.value.length);
            text = (y)? to_latin(text_var.value.substring(start, end)) : to_cyrillic(text_var.value.substring(start, end));
            text_var.value = left + text + right;
            text_var.selectionStart = start;
            text_var.selectionEnd = start + text.length;
        }
        else {
            left = text_var.value.substring(0, start);
            left = (y)? to_latin(left) : to_cyrillic(left);
            text_var.value = (y)? to_latin(text_var.value) : to_cyrillic(text_var.value);
            text_var.selectionStart = text_var.selectionEnd = left.length;
        }
        text_var.scrollTop=top;
    }
    else
    if (document.selection) {
        text_var.focus();
        var sel = document.selection.createRange();
        if (sel.text) {
            sel.text = (y)? to_latin(sel.text) : to_cyrillic(sel.text);
        }
        else
            text_var.value = (y)? to_latin(text_var.value) : to_cyrillic(text_var.value);
    }
    else
        text_var.value = (y)? to_latin(text_var.value) : to_cyrillic(text_var.value);
    text_var.focus();
}

function to_cyrillic(text){
    var cyrillic_text = "";
    for (i=0;i<text.length;i++) {
        if ( list[0][text.substr(i,1)])
            cyrillic_text +=  list[0][text.substr(i,1)];
        else
            cyrillic_text += text.substr(i,1);
    }
    return cyrillic_text;
}

function to_latin(text){
    var latin_text = "";
    for (i=0;i<text.length;i++) {
        if ( list[1][text.substr(i,1)])
            latin_text += list[1][text.substr(i,1)];
        else
            latin_text += text.substr(i,1);
    }
    return latin_text;
}
// Translit-Script-End
